//
//  PhotoBrowserAnimator.swift
//  新浪微博
//
//  Created by 李赛 on 15/5/21.
//  Copyright (c) 2015年 李赛. All rights reserved.
//

import UIKit

class PhotoBrowserAnimator: NSObject ,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{

    // 是否modal
    var isPresent = false
    // 转场临时视图
    var dummyView : UIView?
    // 转场的启动位置
    var dummyStartRect = CGRectZero
    // 转场的结束位置
    var dummyEndRect = CGRectZero
    
    // 制定谁负责转场动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 制定谁负责解除转场动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
    
    // 制定转场时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval {
        return 1.0
    }
    
    /// 转场的细节
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let duration = transitionDuration(transitionContext)
        
        // 判断是转场还是解除转场
        if isPresent {
            // modal 动画处理
            // 找到目标视图
            if let toView = transitionContext.viewForKey(UITransitionContextToViewKey) {
                // 转场的临时视图
                transitionContext.containerView().addSubview(dummyView!)
                UIView.animateWithDuration(duration, animations: { () -> Void in
                    self.dummyView!.frame = self.dummyEndRect
                }, completion: { (_) -> Void in
                    // 删除临时视图
                    self.dummyView!.removeFromSuperview()
                    // 动画完成之后  添加目标视图
                    transitionContext.containerView().addSubview(toView)
                    // 标示转场结束
                    transitionContext.completeTransition(true)
                })
            }
        } else {
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! PhotoBrowserViewController
            let photoCell = fromVC.collectionView.visibleCells().last as! PhotoBrowserCell
            let imageView = photoCell.viewerVC!.view
            
            // 抓取快照  合成一个视图
            let dummy = imageView.snapshotViewAfterScreenUpdates(false)
            // 隐藏fromView
            fromView?.hidden = true
            
            let rect = CGRectMake(CGRectGetMidX(dummyStartRect), CGRectGetMidY(dummyStartRect), 0, 0)
            
            // 将fromView 添加到容器视图
            transitionContext.containerView().addSubview(dummy)
            
            UIView.animateWithDuration(duration, animations: { () -> Void in
                dummy.frame = rect
            }, completion: { (_) -> Void in
                transitionContext.completeTransition(true)
            })
        }
    }
}
